package com.zhiche.lisa.bms.service.invoice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.invoice.InvoiceApplyAsseMapper;
import com.zhiche.lisa.bms.dao.model.fee.FeeAssessment;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApplyAsse;
import com.zhiche.lisa.bms.service.fee.FeeAssessmentService;
import com.zhiche.lisa.bms.service.invoice.InvoiceApplyAsseService;
import com.zhiche.lisa.core.supports.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 申请开票明细-考核 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-09-27
 */
@Service
public class InvoiceApplyAsseServiceImpl extends ServiceImpl<InvoiceApplyAsseMapper, InvoiceApplyAsse> implements InvoiceApplyAsseService {
    @Autowired
    private FeeAssessmentService feeAssessmentService;

    /**
     * 根据头单id获取申请考核开票详明
     *
     * @param headerId
     * @return
     */
    @Override
    public List<FeeAssessment> selectInvoiceApplyAsseListByHeaderId(Long headerId) {
        try {
            if (Objects.isNull(headerId)) {
                throw new BaseException("请传入需要匹配的id");
            }
            EntityWrapper<InvoiceApplyAsse> invoiceApplyBillEw = new EntityWrapper<>();
            invoiceApplyBillEw.eq("head_id", headerId);
            List<InvoiceApplyAsse> invoiceApplyAsses = this.selectList(invoiceApplyBillEw);
            List<String> codes = new ArrayList<>();
            if (!CollectionUtils.isEmpty(invoiceApplyAsses)) {
                invoiceApplyAsses.forEach(invoiceApplyAsse -> codes.add(invoiceApplyAsse.getAssessCode()));
            }
            if (!CollectionUtils.isEmpty(codes)) {
                return feeAssessmentService.selectFeeAssessmentListByCodes(codes);
            } else {
                return null;
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("获取申请考核开票详明异常");
        }
    }

    /**
     * 批量新增申请考核开票详明
     *
     * @param invoiceApplyAsses
     * @param headerId
     * @return
     */
    @Override
    public void insertInvoiceApplyAsse(List<InvoiceApplyAsse> invoiceApplyAsses, Long headerId, Integer type) {
        try {
            if (Objects.isNull(headerId)) {
                throw new BaseException("请传入需要匹配的id");
            }
            if (!CollectionUtils.isEmpty(invoiceApplyAsses)) {
                for (InvoiceApplyAsse invoiceApplyAsse : invoiceApplyAsses) {
                    invoiceApplyAsse.setHeadId(headerId);
                    invoiceApplyAsse.setType(type);
                }
                this.insertBatch(invoiceApplyAsses);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("申请考核开票详明新增异常");
        }
    }

    /**
     * 根据头表id查询
     *
     * @param headId
     * @return
     */
    @Override
    public List<InvoiceApplyAsse> selectInvoiceApplyAssessByHeadId(Long headId) {
        EntityWrapper<InvoiceApplyAsse> invoiceApplyAssessEw = new EntityWrapper<>();
        invoiceApplyAssessEw.eq("head_id", headId);
        return this.selectList(invoiceApplyAssessEw);
    }

    /**
     * 批量删除
     *
     * @param headId
     */
    @Override
    public void deleteAssessDetailByHeadId(Long headId) {
        if (Objects.nonNull(headId)) {
            EntityWrapper<InvoiceApplyAsse> ew = new EntityWrapper<>();
            ew.eq("head_id", headId);
            this.delete(ew);
        }
    }
}
